Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

156
Vistas
Cannot send data from aspx file to code behind using $.ajax({ type: "POST", using VS2017 C#

I'm using web-forms to collect data from a form and then send the data to the code-behind to send to an API. Using a button I'm calling a JavaScript method which collates the data and then sends to my aspx.cs file to send to the API. The Html code for the button is

<button class="search btn" ID="btnSearch" onclick="searchApi(); return false;"><i class="fas fa-search"></i>Search</button>

This runs the searchAPI() function which works and creates a concatenated string called SearchData. The Javascript code looks like this

var searchString = JsonData;
var trimsearchString = searchString.replace(/,/g, '');

$.ajax({
   type: "POST",
   url: 'Default.aspx/GetApi',
      data: searchString,
      contentType: "application/json; charset=utf-8",
      dataType: "json",
      success: function (data) {
          alert('success');
                },
                error: function (errordata) {
                    console.log(errordata);
                    alert(errordata);
                }
    });

The method GetAPI in my Default.aspx.cs file is never called. The method code is

[System.Web.Services.WebMethod]
public static void GetApi(string searchData)    
{...

The success: function (data) returns success but the code behind method is never called, can someone please tell me what I am missing.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

fix the ajax data, it seems that method with this parameter can' t be found

$.ajax({
   type: "POST",
   url: 'Default.aspx/GetApi',

      data: { searchData: trimsearchString},
      //or if it is still problem, you can even try
       data: JSON.stringify( { searchData: trimsearchString}),

      contentType: "application/json; charset=utf-8",
      dataType: "json",
      success: function (data) {
          alert(data);
                },
                error: function (errordata) {
                    console.log(errordata);
                    alert(errordata);
                }
    });

and your webmethod should return something

[WebMethod]
public static string GetApi(string searchData)    
{
   return searchData
}
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda